package org.summerframework.component.upload.file.controller;

import org.summerframework.component.security.controller.BaseController;
import org.summerframework.component.upload.FileUploadService;
import org.summerframework.component.upload.file.service.FileUserRelService;
import org.summerframework.component.upload.file.service.dto.FileUserRelDTO;
import org.summerframework.component.upload.file.service.dto.FileUserRelQueryDTO;
import org.summerframework.component.upload.type.FileUploadDTO;
import org.summerframework.core.base.result.ResultDTO;
import org.summerframework.core.page.PageResult;
import org.summerframework.core.util.Base64Util;
import org.summerframework.core.util.OptionalUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(tags = "文件管理", description = "功能描述")
@RequestMapping("file")
public class FileUserRelController extends BaseController {
    @Resource
    private FileUserRelService fileUserRelService;

    @Resource
    private FileUploadService fileUploadService;

    @ApiOperation(value = "文件访问, 访问指定的压缩类型", notes = "uploadFile说明   图片: thumb 音频: MP3, WAV, AMR  视频: MP4, OGV")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "可以随便是什么, file", value = "文件流, 多个文件, 以接收的顺序返回", required = true, dataType = "fromData", paramType = "query", dataTypeClass = MultipartHttpServletRequest.class),
    })
    @PostMapping("upload")
    public ResultDTO<List<FileUserRelDTO>> imageUpload(MultipartHttpServletRequest request, FileUploadDTO fileBaseUploadDTO) {
        fileBaseUploadDTO.setUserId(getUserId() != null ? getUserId() : 1L);
        return ResultDTO.ok(FileUploadDTO.request(request, fileBaseUploadDTO).stream().map(fileUploadService::uploadFile).collect(Collectors.toList()));
    }

    @ApiOperation(value = "文件访问, 访问指定的压缩类型", notes = "uploadFile说明   图片: thumb 音频: MP3, WAV, AMR  视频: MP4, OGV")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件流", required = true, dataType = "fromData", paramType = "query", dataTypeClass = MultipartFile.class),
    })
    @PostMapping("upload/one")
    public ResultDTO<FileUserRelDTO> imageUpload(MultipartFile file, FileUploadDTO fileBaseUploadDTO) {
        fileBaseUploadDTO.setUserId(getUserId() != null ? getUserId() : 1L);
        fileBaseUploadDTO.setFile(file);
        return ResultDTO.ok(fileUploadService.uploadFile(fileBaseUploadDTO));
    }

    @ApiOperation(value = "文件访问, 访问指定的压缩类型", notes = "uploadFile说明   图片: thumb 音频: MP3, WAV, AMR  视频: MP4, OGV")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "base64", value = "base64文件流", required = true, paramType = "query", dataTypeClass = String.class),
            @ApiImplicitParam(name = "fileName", value = "文件名称", required = true, paramType = "query", dataTypeClass = String.class),
    })
    @PostMapping("upload/base64")
    public ResultDTO<?> imageUpload(String base64, String fileName, FileUploadDTO fileBaseUploadDTO) {
        int i;
        if ((i = base64.indexOf(";base64,")) > -1) {
            base64 = base64.substring(i + 8);
        }

        fileBaseUploadDTO.setFileBytes(Base64Util.decodeAsByte(base64));
        fileBaseUploadDTO.setFileName(fileName);
        fileBaseUploadDTO.setUserId(getUserId() != null ? getUserId() : 1L);
        return ResultDTO.ok(fileUploadService.uploadFile(fileBaseUploadDTO));
    }

    @PostMapping
    @ApiOperation("添加")
    public ResultDTO<Boolean> insert(FileUserRelDTO fileUserRelDTO) {
        return ResultDTO.ok(true, fileUserRelService.insert(fileUserRelDTO));
    }

    @PutMapping
    @ApiOperation("更新")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", required = true, dataType = "Long", paramType = "query")})
    public ResultDTO<Integer> updateById(FileUserRelDTO fileUserRelDTO) {
        return ResultDTO.ok(fileUserRelService.updateById(fileUserRelDTO));
    }

    @DeleteMapping("{id}")
    @ApiOperation("根据id删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", required = true, dataType = "Long", paramType = "path")})
    public ResultDTO<Integer> deleteById(@PathVariable("id") Long id) {
        return ResultDTO.ok(fileUserRelService.deleteById(id));
    }

    @DeleteMapping
    @ApiOperation("根据id列表删除删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "根据ids删除", required = true, dataType = "List", paramType = "query")})
    public ResultDTO<Integer> deleteByIds(@RequestParam("ids") List<Long> ids) {
        return ResultDTO.ok(fileUserRelService.deleteByIds(ids));
    }

    @GetMapping("{id}")
    @ApiOperation("根据id查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", required = true, dataType = "Long", paramType = "path")})
    public ResultDTO<FileUserRelDTO> selectById(@PathVariable("id") Long id) {
        return ResultDTO.ok(OptionalUtils.get(fileUserRelService.selectById(id)));
    }

    @GetMapping
    @ApiOperation("根据query查询")
    public PageResult<FileUserRelDTO> selectPageResultByQuery(FileUserRelQueryDTO fileUserRelQueryDTO) {
        return fileUserRelService.selectPageResultByQuery(fileUserRelQueryDTO);
    }
}
